$(function(){
	dictionaryStart("model","13001");
	dictionaryStart("productionfactory","13002");
	//xlStart();
	$("#linecode").bind('change',function(){
		gtStart();
	});
	$("#towercode").bind('change',function(){
		fallow();
	});
	$("#thenumber").bind('blur',function(){
		gettotalcapacity();
	});
	$("#singlecapacity").bind('blur',function(){
		gettotalcapacity();
	});
});

var xlStart=function(){
	$.ajax({
		url : contextPath+'user/dyxl/dlist',
		type : 'post',
		data : {
			companyId : companyId,
			devicetype : byqtype,
			transformercode : transformercode
		},
		async : false,
		success : function(data){
			if(data!=null&&data.length>0){
				var option="";
				$("#linecode").empty().show();
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#linecode").append(option);
			}
			setTimeout(function(){
				gtStart();
			},200);
		}
	});
}

var gtStart=function(){
	var linecode=$("#linecode").val();
	$.ajax({
		url : contextPath+'user/dygt/dlist',
		type : 'post',
		data : {
			companyId : companyId,
			deviceid : linecode
		},
		async : false,
		success : function(data){
			if(data!=null&&data.length>0){
				$("#towercode").empty().show();
				var option="";
				for(var i=0;i<data.length;i++){
					option += '<option value="'+data[i].deviceid+'">'+data[i].devicename+'</option>';
				}
				$("#towercode").append(option);
			}
		}
	});
	/*$.ajax({
		url : contextPath+'user/dygt/load',
		type : 'post',
		data : {
			deviceId : linecode
		},
		success :function(data){
			if(data!=null){
				$("#voltagelevel").val(data.voltagelevel);
			}
		}
	});*/
}

function fallow(){
	var gtId=$('#towercode').val();
	deviceid=gtId;
	$.ajax({
		url : contextPath+'user/dygt/load',
		data : {
			deviceid : gtId
		},
		type : 'post',
		contentType: "application/x-www-form-urlencoded; charset=utf-8", 
		success : function(data){
			if(data!=null){
				$('#localcity').val(data.localcity);
				$('#operationunit').val(data.operationunit);
				$('#maintenanceteam').val(data.maintenanceteam);
			}
		}
	});
}

function add(){
	var devicename=$("#devicename").val();
	var localcity = $("#localcity").val();
	var voltagelevel = $("#voltagelevel").val();
	var groundingresistance=$("#groundingresistance").val();
	var singlecapacity = $("#singlecapacity").val();
	var thenumber = $("#thenumber").val();
	var groupcapacity = $("#groupcapacity").val();
	if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	/*if(localcity==""){
		alert("所属地市为空");
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return;
	}
	if(!isVoltage(voltagelevel)){
		alert("电压等级不正确，如220v或1kv");
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return;
	}
	if(!isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压不正确，如220v或1kv");
		return;
	}
	if(!isNum(groundingresistance)){
		alert("接地电阻只能为数字");
		return;
	}
	if(!validateNum(singlecapacity)){
		alert("单只容量只能为数字");
		return;
	}
	if(!validateInt(thenumber)){
		alert("台数只能为整数");
		return;
	}
	if(!validateNum(groupcapacity)){
		alert("整组容量只能为数字");
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/dyzsdrq/add",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/d/dyzsdrqlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function edite(){
	var devicename=$("#devicename").val();
	var localcity = $("#localcity").val();
	var voltagelevel = $("#voltagelevel").val();
	var groundingresistance=$("#groundingresistance").val();
	var singlecapacity = $("#singlecapacity").val();
	var thenumber = $("#thenumber").val();
	var groupcapacity = $("#groupcapacity").val();
	/*if(devicename==""){
		alert('设备名称为空');
		return;
	}
	if(!validateLon($(":input[name='longitude']").val())){
		alert("经度范围为-180到180");
		return;
	}
	if(!validateLat($(":input[name='latitude']").val())){
		alert("纬度范围为-90到90");
		return;
	}
	if(localcity==""){
		alert("所属地市为空");
		return;
	}
	if($("input[name='operationunit']").val()==""){
		alert("运维单位为空");
		return;
	}
	if($("input[name='maintenanceteam']").val()==""){
		alert("维护班组为空");
		return;
	}
	if(!isVoltage(voltagelevel)){
		alert("电压等级不正确，如220v或1kv");
		return;
	}
	if($("input[name='commissioningdate']").val()==""){
		alert("投运日期为空");
		return;
	}
	if(!isVoltage($("input[name='ratedvoltage']").val())){
		alert("额定电压不正确，如220v或1kv");
		return;
	}
	if(!isNum(groundingresistance)){
		alert("接地电阻只能为数字");
		return;
	}
	if(!validateNum(singlecapacity)){
		alert("单只容量只能为数字");
		return;
	}
	if(!validateInt(thenumber)){
		alert("台数只能为整数");
		return;
	}
	if(!validateNum(groupcapacity)){
		alert("整组容量只能为数字");
		return;
	}*/
	var list=$("#form").serialize();
	$.ajax({
		url : contextPath+"user/dyzsdrq/edite",
		data : {data : list},
		type : "post",
		success : function(data){
			alert(data.msgDesc);
			if(data.opResult=="0"){
				location.href=contextPath+"user/d/dyzsdrqlist.jsp?deviceid="+deviceid+"&t="+new Date().getTime();
			}
		}
	});
}

function setDefault(elementId,data){
	$('#'+elementId).val(data);
}

function gettotalcapacity(){
	var singlecapacity=$('#singlecapacity').val();
	if(singlecapacity==null||singlecapacity==""){
		alert("请输入单只容量");
		return;
	}
	var thenumber=$("#thenumber").val();
	$("#groupcapacity").val(thenumber*parseFloat(singlecapacity));
	
}